struct Solution;
// 这个是错的

impl Solution {
    pub fn pattern_matching(pattern: String, value: String) -> bool {
        let mut nums = [0, 0]; // a, b 的个数
        for c in pattern.as_bytes() {
            nums[(*c - 'a' as u8) as usize] += 1;
        }

        let mut len = [-1, 0];
        if nums[0] == 0 || nums[1] == 0 {
            let num = nums[0] + nums[1];
        }
        // 如果 a_num or b_num == 0, 下面会出错
        'out: while len[0] <= (value.len() / nums[0]) {
            len[0] += 1;
            if (value.len() - len[0] * nums[0]) % nums[1] != 0 {
                // 如果不为0, 表示这时候结果的长度不对
                continue;
            }
            len[1] = (value.len() - len[0] * nums[0]) / nums[1];
            let mut value_index = 0;
            let mut p = [None, None];
            'inloop: for c in pattern.as_bytes() {
                let i = (*c - 'a' as u8) as usize;
                match p[i] {
                    Some(s) => {
                        if &value[value_index..(value_index + len[i])] == s {
                            continue 'inloop;
                        } else {
                            continue 'out;
                        }
                    }
                    None => p[i] = Some(&value[value_index..(value_index + len[i])]),
                }
            }
            return true;
        }
        false
    }
}

fn main() {
    let pattern = "ab";
    let value = "dogcat";
    if Solution::pattern_matching(pattern.into(), value.into()) {
        println!("yes, it is");
    }
}
